package com.advaita.licenseManager;

import java.awt.EventQueue;
import javax.swing.JFrame;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.UIManager;

import com.advaita.license.Customer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LicenseManager {

	private JFrame frame;
	private final JPanel panelMain2 = new JPanel(new BorderLayout());
	private final JPanel panelMain1 = new JPanel(new BorderLayout());

	private final JPanel panelButtons1 = new JPanel();
	private final JPanel panelButtons2 = new JPanel();

	private CustomerInfo custInfo = null;
	private CustomerList customerList;

	private boolean updateList = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					// Set up the Nimbus look and feel
					UIManager.LookAndFeelInfo plafinfo[] = UIManager
							.getInstalledLookAndFeels();
					boolean nimbusfound = false;
					int nimbusindex = 0;

					for (int look = 0; look < plafinfo.length; look++) {
						if (plafinfo[look].getClassName().toLowerCase()
								.contains("nimbus")) {
							nimbusfound = true;
							nimbusindex = look;
						}
					}

					try {

						if (nimbusfound) {
							UIManager.setLookAndFeel(plafinfo[nimbusindex]
									.getClassName());
						} else
							UIManager.setLookAndFeel(UIManager
									.getCrossPlatformLookAndFeelClassName());

					} catch (Exception e) {
					}

					LicenseManager window = new LicenseManager();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LicenseManager() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 792, 468);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));

		frame.getContentPane().add(panelButtons1);

		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Customer cust = customerList.GetSelectedCustomer();
				updateList = true;
				LoadCustomerInfo(cust);
			}
		});
		panelButtons1.add(btnEdit);

		JButton btnNew = new JButton("New");
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateList = false;
				Customer cust = new Customer();
				Customer.populateBlankCustomer(cust);
				LoadCustomerInfo(cust);
			}
		});
		panelButtons1.add(btnNew);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SaveCustomer();
			}
		});
		panelButtons2.add(btnSave);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CancelPressed();
			}
		});
		panelButtons2.add(btnCancel);

		LoadCustomerList();
	}

	private void LoadCustomerList() {
		frame.getContentPane().remove(panelMain2);
		frame.getContentPane().remove(panelButtons2);
		frame.getContentPane().add(panelButtons1, BorderLayout.SOUTH);

		panelMain1.removeAll();
		customerList = new CustomerList();
		panelMain1.add(customerList, BorderLayout.CENTER);

		frame.getContentPane().add(panelMain1, BorderLayout.CENTER);
		frame.getContentPane().invalidate();
		frame.getContentPane().validate();
		frame.getContentPane().repaint();
	}

	private void LoadCustomerInfo(Customer cust) {
		frame.getContentPane().remove(panelMain1);
		frame.getContentPane().remove(panelButtons1);
		frame.getContentPane().add(panelButtons2, BorderLayout.SOUTH);
		
		panelMain2.removeAll();
		custInfo = new CustomerInfo(cust);
		panelMain2.add(custInfo, BorderLayout.CENTER);
		
		frame.getContentPane().add(panelMain2, BorderLayout.CENTER);
		frame.getContentPane().invalidate();
		frame.getContentPane().validate();
		frame.getContentPane().repaint();
	}

	private void SaveCustomer() {
		if (custInfo == null)
			return;

		Customer cust = custInfo.UpdateCustomer();
		customerList.UpdateCustomer(cust);
		LoadCustomerList();
	}

	private void CancelPressed() {
		LoadCustomerList();
	}
}
